<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">

    <script src="js/jquery.min.js"></script>
    <script src="js/logout.js"></script>
    <script src="js/urlutils.js"></script>


    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <!-- <a href="login.html">登陆</a> -->
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id="blog_list">

            </div>
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">正文</div>
                <div class="view">
                    <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>
                </div>
            </div> -->
           
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="tofirst()">首页</button> 
                <button class="blog-pagnation-item" onclick="tobefore()">上一页</button>
                <span id="pindex">1</span>/<span id="pcount">1</span>
                <button class="blog-pagnation-item" onclick="tonext()">下一页</button>
                <button class="blog-pagnation-item" onclick="tolast()">末页</button>
            </div>
        </div>
    </div>
    <script>
        // 初始化页面
        var pSize = 3; // 每页显示条数
        var pIndex = 1 // 页码
        var pageCount = 1 // 总页数
        function init() {
            // 1.处理分页参数
            pIndex = getParamValue("pIndex");
            if (pIndex == null) {
                pIndex = 1;
            }
            pSize = getParamValue("pSize");
            if (pSize == null) {
                pSize = 3;
            }
            jQuery("#pindex").html(pIndex);
            // 2.向后端请求数据
            jQuery.ajax({
                url:"/art/getlistbypage",
                type:"GET",
                data:{
                    "pIndex":pIndex,
                    "pSize":pSize
                },
                // 3.将结果返回给用户
                success:function(res){
                    if (res.code == 200 && res.data != null) {
                        // 获取数据成功
                        var createHtml = "";
                        if (res.data.articles != null || res.data.articles.length > 0) {
                            // 文章列表不为空
                            pageCount = res.data.pageCount;
                            jQuery("#pcount").html(pageCount);
                            var artlist = res.data.articles;
                            for(var i=0;i<artlist.length;i++){
                                var art = artlist[i]; // 文章对象
                                createHtml += '<div class="blog" >';
                                createHtml += '<div class="title">'+art.title+'</div>';
                                createHtml += '<div class="date">'+art.createtime+'</div>';
                                createHtml += '<div class="desc">'+art.content+'</div>';
                                createHtml += '<div class="view">';
                                createHtml += '<a href="blog_content.html?aid='+
                                    art.id+'" class="detail">查看全文 &gt;&gt;</a>';
                                createHtml += '</div>';
                                createHtml += '</div>';
                            }
                        } else {
                            // 文章列表为空
                            createHtml += "<h3 style='text-align: center; margin-top:10px'>暂无文章</h3>";
                        }
                        jQuery("#blog_list").html(createHtml);
                    } else {
                        alert("查询失败:"+res.meg);
                    }
                }
            });

        }
        init();

        // 点击首页
        function tofirst(){
            // 1.判断是否在首页
            if (pIndex == 1) {
                // 无需跳转，已经在首页了
                return false;
            }
            // 2.跳转到首页
            location.href = "blog_list.html";
        }
        // 点击末页
        function tolast(){
            // 1.判断是否在末页
            if (pIndex == pageCount) {
                // 无需跳转，已经在末页了
                return false;
            }
            // 2.跳转到末页
            location.href = "blog_list.html?pIndex="+pageCount;
        }
        // 点击下一页
        function tonext(){
            // 1.判断是否在末页
            if (pIndex == pageCount) {
                // 无需跳转，已经在末页了
                return false;
            }
            // 2.跳转到下一页
            location.href = "blog_list.html?pIndex="+(parseInt(pIndex)+1);
        }
        // 点击上一页
        function tobefore(){
            // 1.判断是否在首页
            if (pIndex == 1) {
                // 无需跳转，已经在末页了
                return false;
            }
            // 2.跳转到上一页
            location.href = "blog_list.html?pIndex="+(parseInt(pIndex)-1);
        }

    </script>
</body>
</html>